package com.ruoyi.livesass.api.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.livesass.LiveVideoService;
import com.ruoyi.livesass.api.sync.LiveTrafficDataSynService;
import com.ruoyi.livesass.api.sync.SysConfigUtils;
import com.volcengine.model.livesaas.response.GetLoginLivesaasStsResponse;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiOperation;


/**
* @ClassName: ZbjDpDataController 
* @Description: TODO(直播间管理) 
* @author 晏大兵 
* @date 2025年3月10日 下午3:02:46 
 */
@RestController
@RequestMapping("/api/zbjgl")
public class ZbjGlDataController extends BaseController {
	
	private static final Logger LOG = LoggerFactory.getLogger(LiveTrafficDataSynService.class);
	
	
	@Autowired
	LiveVideoService liveVideoService;
	
	@Autowired
	SysConfigUtils sysConfigUtils;
	
	

	   @ApiOperation("根据直播间ID返回嵌入地址")
	    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:edit')")
	    @PostMapping("/getZjbUrl")
	    public ResponseEntity<Map<String,String>> getZjbUrl(long zbjid) throws Exception {
	    	String baseUrl = "https://vlive.byteoc.com/livesaas/embeded/" + zbjid +"?";
	    	Map<String,String> resultData = Maps.newHashMap();
	    	System.out.println(getDeptId());
	    	String subName = sysConfigUtils.getSubUserName(getDeptId());
	    	if(StrUtil.isBlankIfStr(subName)) {
	    		throw new RuntimeException("直播间子账户未配置正确，请联系管理员!");
	    	}
	    	
	    	GetLoginLivesaasStsResponse getLoginLivesaasStsResponse = liveVideoService.getLivesaasSts(subName);

	    	String loginLivesaasToken = getLoginLivesaasStsResponse.getResult().getLoginLivesaasToken();
	    	String accessKeyId = getLoginLivesaasStsResponse.getResult().getStsMsg().getAccessKeyId();
	    	String secretAccessKey = getLoginLivesaasStsResponse.getResult().getStsMsg().getSecretAccessKey();
	    	String sessionToken = getLoginLivesaasStsResponse.getResult().getStsMsg().getSessionToken();
	    	
	    	//&hideLoopVideo=true TODO 伪直播-视频轮播  &hidePopularity=true  --放开人气设置
	    	
	    	String hideControl = "hideWebPush=true&hideMobileBroadcast=true&hideLivePartner=true&hideCloudCast=true&hideBasicAiSubtitle=true&hideDiligentDetect=true&hideLiveScreenData=true&hideVideoEdit=true";
	    	
	    	String videoUrl = baseUrl + "type=LiveControl&appid=xehx&AccessKeyId=" + accessKeyId + "&SecretAccessKey=" + secretAccessKey
	    			+ "&SessionToken=" + sessionToken + "&LoginLivesaasToken=" + loginLivesaasToken + "&" +hideControl;
	    	
	    	String chartUrl = baseUrl + "type=ChatInteraction&appid=xehx&AccessKeyId=" + accessKeyId + "&SecretAccessKey=" + secretAccessKey
	    			+ "&SessionToken=" + sessionToken + "&LoginLivesaasToken=" + loginLivesaasToken ;
	    	
	    	
	    	resultData.put("videoUrl", videoUrl);
	    	resultData.put("chartUrl", chartUrl);
	    	
	        return ResponseEntity.ok(resultData);
	    }
    
	
    @ApiOperation("根据直播间ID返回嵌入地址")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:edit')")
    @PostMapping("/getHfglUrl")
    public ResponseEntity<Map<String,String>> getHfglUrl(long zbjid) throws Exception {
    	String baseUrl = "https://vlive.byteoc.com/livesaas/embeded/" + zbjid +"?";
    	Map<String,String> resultData = Maps.newHashMap();
    	System.out.println(getDeptId());
    	String subName = sysConfigUtils.getSubUserName(getDeptId());
    	if(StrUtil.isBlankIfStr(subName)) {
    		throw new RuntimeException("直播间子账户未配置正确，请联系管理员!");
    	}
    	
    	GetLoginLivesaasStsResponse getLoginLivesaasStsResponse = liveVideoService.getLivesaasSts(subName);

    	String loginLivesaasToken = getLoginLivesaasStsResponse.getResult().getLoginLivesaasToken();
    	String accessKeyId = getLoginLivesaasStsResponse.getResult().getStsMsg().getAccessKeyId();
    	String secretAccessKey = getLoginLivesaasStsResponse.getResult().getStsMsg().getSecretAccessKey();
    	String sessionToken = getLoginLivesaasStsResponse.getResult().getStsMsg().getSessionToken();
    	
    	//&hideLoopVideo=true TODO 回放管理
    	
    	String hideControl = "hideReplayConfig=true&hideUploadVideoBtn=true&hideImportFromVideoLib=true";
    	
    	String videoUrl = baseUrl + "type=ReplayManagement&appid=xehx&AccessKeyId=" + accessKeyId + "&SecretAccessKey=" + secretAccessKey
    			+ "&SessionToken=" + sessionToken + "&LoginLivesaasToken=" + loginLivesaasToken + "&" +hideControl;
    	
    	
    	
    	//ChatInteraction
    	
    	
    	resultData.put("videoUrl", videoUrl);
    	
        return ResponseEntity.ok(resultData);
    }
    


}
